package service

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"strings"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateSelectList
//@description: 创建SelectList记录
//@param: selectList model.SelectList
//@return: err error

func CreateSelectList(selectList model.SelectList) (err error) {
	id, err := GetLastSelectID()
	if err != nil {
		selectList.SelectGoodsId = model.SelectListIdPrefix + 1
	}else{
		selectList.SelectGoodsId = id + 1
	}
	selectList.SelectRecommendationLevel = 0
	selectList.SelectReferrer = ""
	err = global.GVA_DB.Create(&selectList).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSelectList
//@description: 删除SelectList记录
//@param: selectList model.SelectList
//@return: err error

func DeleteSelectList(selectList model.SelectList) (err error) {
	err = global.GVA_DB.Delete(&selectList).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSelectListByIds
//@description: 批量删除SelectList记录
//@param: ids request.IdsReq
//@return: err error

func DeleteSelectListByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.SelectList{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateSelectList
//@description: 更新SelectList记录
//@param: selectList *model.SelectList
//@return: err error

func UpdateSelectList(selectList model.SelectList) (err error) {
	err = global.GVA_DB.Save(&selectList).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSelectList
//@description: 根据id获取SelectList记录
//@param: id uint
//@return: err error, selectList model.SelectList

func GetSelectList(id uint) (err error, selectList model.SelectList) {
	err = global.GVA_DB.Where("id = ?", id).First(&selectList).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSelectListInfoList
//@description: 分页获取SelectList记录
//@param: info request.SelectListSearch
//@return: err error, list interface{}, total int64

func GetSelectListInfoList(info request.SelectListSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.SelectList{})
    var selectLists []model.SelectList
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.SelectGoodsName != ""{
		db = db.Where("select_goods_name LIKE ?", "%"+info.SelectGoodsName+"%")
	}
	if info.SelectOwner != ""{
		db = db.Where("select_owner = ?", info.SelectOwner)
	}
	if info.CategorySecond != ""{
		db = db.Where("category_second = ?", info.CategorySecond)
	}

	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&selectLists).Error
	return err, selectLists, total
}

/**
获取最新的任务ID
*/
func GetLastSelectID() (int, error) {
	var selectListM model.SelectList
	db := global.GVA_DB.Model(&model.SelectList{})
	err := db.Last(&selectListM).Error
	return selectListM.SelectGoodsId, err
}

func RecommendGoods(info model.SelectList, referrer string) error {
	// 创建db
	db := global.GVA_DB.Model(&model.SelectList{})
	var selectLists model.SelectList
	err := db.Where("select_goods_id = ?", info.SelectGoodsId).First(&selectLists).Error
	if err != nil {
		return err
	}else{
		if strings.Contains(selectLists.SelectReferrer, referrer){
			return fmt.Errorf("已赞过该选品，请勿重复赞")
		}else{
			selectRecommendationLevel := selectLists.SelectRecommendationLevel + 1
			var selectReferrer string
			if selectLists.SelectReferrer == "" {
				selectReferrer = referrer
			}else{
				selectReferrer = selectLists.SelectReferrer + "," + referrer
			}
			// 取消踩
			if strings.Contains(selectLists.SelectUnReferrer, referrer){
				//分数加一
				selectRecommendationLevel++
				unReferrerList := strings.Split(selectLists.SelectUnReferrer, ",")
				var unReferrerLi []string
				for _, v := range unReferrerList{
					if v != referrer{
						unReferrerLi = append(unReferrerLi, v)
					}
				}
				unReferrer := strings.Join(unReferrerLi, ",")
				err := db.Where("select_goods_id = ?", info.SelectGoodsId).First(&selectLists).Update(
					"select_recommendation_level", selectRecommendationLevel).Update(
					"select_referrer", selectReferrer).Update("select_un_referrer", unReferrer).Error
				return err
			}else{
				err := db.Where("select_goods_id = ?", info.SelectGoodsId).First(&selectLists).Update(
					"select_recommendation_level", selectRecommendationLevel).Update(
					"select_referrer", selectReferrer).Error
				return err
			}
		}

	}
}

func UnrecommendGoods(info model.SelectList, referrer string) error {
	// 创建db
	db := global.GVA_DB.Model(&model.SelectList{})
	var selectLists model.SelectList
	err := db.Where("select_goods_id = ?", info.SelectGoodsId).First(&selectLists).Error
	if err != nil {
		return err
	}else{
		if strings.Contains(selectLists.SelectUnReferrer, referrer){
			return fmt.Errorf("已踩过该选品，请勿重复踩")
		}else{
			selectRecommendationLevel := selectLists.SelectRecommendationLevel - 1
			var selectUnReferrer string
			if selectLists.SelectUnReferrer == "" {
				selectUnReferrer = referrer
			}else{
				selectUnReferrer = selectLists.SelectUnReferrer + "," + referrer
			}
			// 取消赞
			if strings.Contains(selectLists.SelectReferrer, referrer){
				// 分数减一
				selectRecommendationLevel--
				referrerList := strings.Split(selectLists.SelectReferrer, ",")
				var referrerLi []string
				for _, v := range referrerList{
					if v != referrer{
						referrerLi = append(referrerLi, v)
					}
				}
				referrer := strings.Join(referrerLi, ",")
				err := db.Where("select_goods_id = ?", info.SelectGoodsId).First(&selectLists).Update(
					"select_recommendation_level", selectRecommendationLevel).Update(
					"select_referrer", referrer).Update("select_un_referrer", selectUnReferrer).Error
				return err
			}else{
				err := db.Where("select_goods_id = ?", info.SelectGoodsId).First(&selectLists).Update(
					"select_recommendation_level", selectRecommendationLevel).Update(
					"select_un_referrer", selectUnReferrer).Error
				return err
			}
		}

	}
}